package com.lcq.study.normal;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName BlockQueueTest
 * @Description
 * @Author lichuanqi
 * @Date 2022/3/21 10:51
 * @Version 1.0
 **/
public class BlockQueueTest {
    public static void main(String[] args) {
        SynchronousQueue<User> synchronousQueue = new SynchronousQueue();
        //synchronousQueue.offer();




        Thread thread1 = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    TimeUnit.SECONDS.sleep(3);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                User user = new User();
                user.setAge(11);
                user.setName("zhangsan");
                System.out.println("添加到队列");
                synchronousQueue.add(user);


                User user1 = new User();
                user1.setAge(11);
                user1.setName("zhangsan");
                System.out.println("添加到队列");
                synchronousQueue.add(user1);
            }
        });

        Thread thread2 = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    System.out.println("等到队列...");
                    User user = synchronousQueue.take();
                    System.out.println(user.getName());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        thread2.start();
        thread1.start();
        //synchronousQueue.offer(,time);
        //synchronousQueue.drainTo();


        //LinkedBlockingQueue<Runnable> linkedBlockingQueue = new LinkedBlockingQueue();
    }


    static class Worker implements Runnable{
        @Override
        public void run() {
            System.out.println("haha");
        }
    }

    static class User {
        private String name;
        private Integer age;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public Integer getAge() {
            return age;
        }

        public void setAge(Integer age) {
            this.age = age;
        }
    }

}
